package com.blog.common.constant;


public enum ResultCode {

    /* 成功状态码 */
    SUCCESS(200, "成功"),
    CREATED(201, "已创建"),
    ACCEPTED(202, "已接受"),

    /* 客户端错误：400-499 */
    BAD_REQUEST(400, "错误的请求"),
    UNAUTHORIZED(401, "未授权"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "未找到"),
    METHOD_NOT_ALLOWED(405, "方法不允许"),
    REQUEST_TIMEOUT(408, "请求超时"),
    CONFLICT(409, "冲突"),

    /* 服务器错误：500-599 */
    INTERNAL_SERVER_ERROR(500, "系统繁忙，请稍后重试"),
    NOT_IMPLEMENTED(501, "尚未实施"),
    BAD_GATEWAY(502, "错误的网关"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    REQUEST_PARAM_ERROR(505,"参数错误"),

    /* 用户错误：2000-2999 */
    USER_NOT_LOGGED_IN(2000, "用户未登录"),
    USER_LOGIN_ERROR(2001, "用户名或密码错误"),
    USER_ACCOUNT_FORBIDDEN(2002, "账号已被禁用"),
    USER_NOT_EXIST(2003, "用户不存在"),
    USER_HAS_EXISTED(2004, "用户名重复"),
    USER_NEED_ACCOUNT(2005,"用户名不能为空"),
    USER_NEED_PASSWORD(2005,"密码不能为空");




    private final Integer code;
    private final String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer code() {
        return this.code;
    }

    public String message() {
        return this.message;
    }

    public static String getMessage(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }
}
